(function(){

var UserCon = require("../../core/user/_con");
var db = require("../../core/db/index");
var config = require("../../core/_include/config");
var ResponObj = require("../_include/ResponObj");
var role = require("./role");

module.exports = {
	prefix:"/user",
	get:{
		//获取登录者的信息
		"/":function *(next){
			try{
				var user  = role.getUser(this);
				this.body = ResponObj("json",user);
			}catch(errorMsg){
				this.body = ResponObj("error",errorMsg);
			}
			yield next;
		},
		//.query.method:"byname"||"byid"
		"/adminScan/:id_or_name":function *(next){
			//get:this.request.query
			//post:this.request.body
			//params:this.params
			var method = this.request.query.method;
			var id_or_name = this.params.id_or_name;
			if (method=="byname") {
				id_or_name = id_or_name.toLowerCase();
				var user = db.find_one(config.dbname.user,{
					info:{
						name:id_or_name
					}
				});
			}else{
				user = db.find_by_id(config.dbname.user,id_or_name);
			}
			if (user) {
				this.body = ResponObj("json",user);
				//TODO:过滤相应的字段
			}else{
				this.body = ResponObj("error","找不到指定的用户")
			}
			yield next;
		},
		//用户退出登录
		"/loginout":function *(){
			try{
				var user  = role.getUser(this);
				//删除loginHash中同用户名的信息
				db.remove_list(config.dbname.loginHash,{
					user_id:user._id
				});
				//清空Cookie
				this.cookies.set("loginSession","");
				this.body = ResponObj("json","用户下线成功");
			}catch(errorMsg){
				this.body = ResponObj("error",errorMsg);
			}
		}
	},
	post:{
		"/register":function *(next){
			var user_info = this.request.body;
			console.log(user_info);
			//TODO:先验证验证码

			//用户名不分大小写
			admin_info.name = admin_info.name.toLowerCase();
			if(db.find_one(config.dbname.user,{
				info:{
					name:user_info.name
				}
			})){
				this.body = ResponObj("error","用户名已经存在");
			}else if (!RegExp("^([\u4E00-\uFA29]|[\uE7C7-\uE7F3]|[a-zA-Z0-9])*$").test(user_info.name)) {
				this.body = ResponObj("error","用户名格式错误");
			}else if(!(user_info.password&&user_info.confirm_password)){
				this.body = ResponObj("error","表单不完整");
			}else if(user_info.password !== user_info.confirm_password){
				this.body = ResponObj("error","两次密码输入不匹配");
			}else{
				delete user_info.confirm_password;
				new UserCon(user_info);
				this.body = ResponObj("json","用户注册成功");
			}
			yield next;
		},
		//用户登录
		//.body:user_info
		"/login":function *(next){
			var form_data = this.request.body;
			try{
				var user = UserCon.login(form_data);
				this.body = ResponObj("json","登陆成功");
				//删除loginHash中同用户名的信息
				db.remove_list(config.dbname.loginHash,{
					user_id:user._id
				});
				//注入新的Session
				var hash = user._id.substr(2)+(+new Date).toString(16);
				this.cookies.set("loginSession",hash);
				db.insert(config.dbname.loginHash,{
					_id:hash,
					user_id:user._id,
					type:"user",
					beginTime:+new Date
				});
			}catch(errorMsg){
				this.body = ResponObj("error",errorMsg);
			}
			yield next;
		},
		//修改管理员信息附加
		"/update":function(){

		}
	},
	put:{

	},
	"delete":{

	}
}

}());